package it.unisa.unibox.gestioneProdotti;


import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * Servlet implementation class LoginServlet
 */
public class CaricaFileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public static ServletContext servletContext;

	private boolean isMultipart;
	private String filePath;
	private int maxFileSize = 10*1000 * 1024;
	private int maxMemSize = maxFileSize;
	private File file ;     



	public CaricaFileServlet() {
		super();
	}

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	@Override
	public void init(ServletConfig config) throws ServletException {
		try {
			super.init(config);			
			servletContext = getServletConfig().getServletContext();
			filePath = servletContext.getRealPath("files");
		} 

		catch (Exception e) {
			e.printStackTrace();
		}
	}


	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request,response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		response.setHeader("Cache-Control",	"no-store, no-cache, must-revalidate");
		PrintWriter out = response.getWriter();
		isMultipart = ServletFileUpload.isMultipartContent(request);
		if(isMultipart){
			if(uploadFile(request))			
				out.write(file.getName());
			else
				out.write("errore");
		}
		
	}

	

	protected boolean uploadFile(HttpServletRequest request){

		isMultipart = ServletFileUpload.isMultipartContent(request);
		if(!isMultipart)
			return false;

		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setSizeThreshold(maxMemSize);
		factory.setRepository(new File(filePath));

		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setSizeMax(maxFileSize);

		try{ 

			List<?> fileItems = upload.parseRequest(request);
			Iterator<?> i = fileItems.iterator();
			while ( i.hasNext () ) 
			{
				FileItem fi = (FileItem)i.next();
				if ( !fi.isFormField () )	
				{
					String fileName = fi.getName();
					if( fileName.lastIndexOf("\\") >= 0 )
						file = new File( filePath + File.separator + fileName.substring(fileName.lastIndexOf("\\"))) ;
					else
						file = new File( filePath + File.separator + fileName.substring(fileName.lastIndexOf("\\")+1)) ;
					System.out.println(file.getAbsolutePath());
					fi.write(file) ;
					return true;
				}
			}
			return false;
		}catch(Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}
	
	
	
	
}
